package com.frogs.ucenter.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.ArrayUtils;
import org.jsoup.Connection.Method;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.frogs.framework.util.MapUtil;
import com.frogs.framework.util.StringUtil;
import com.frogs.system.util.ApiUrlParamEnum;
import com.frogs.system.web.api.entity.ApiUrl;
import com.frogs.system.web.api.response.Response.ApiResultEnum;
import com.frogs.system.web.api.response.Response.ResultParamEnum;
import com.frogs.ucenter.entity.UserBaseAccountInfo;
import com.frogs.ucenter.service.UserService;

@Service("userService")
public class UserServiceImpl extends UserBaseServiceImpl implements UserService {
	
	private static Logger log = LoggerFactory.getLogger(UserServiceImpl.class);

	@Override
	public JSONObject updateUserBaseInfo(Map<String,Object> userBaseInfo) {
		ApiUrl apiUrl = this.buildUcenterApiBaseUrl("USER_BASE_INFO_UPDATE");
		String[] otherParams = MapUtil.convertToStringArray(userBaseInfo);
		String[] allParams = (String[])ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		
		JSONObject result = null;
		try{
			result = (JSONObject)this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
			if (ApiResultEnum.SUCCESS.getId() != result.getJSONObject("response").getIntValue(ResultParamEnum.code.getParamName()))
				throw new IllegalStateException(result.getJSONObject("response").getString(ResultParamEnum.tip.getParamName()));
		} catch (Exception e) {
			log.error("func[updateUserBaseInfo] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(),Arrays.deepToString(allParams), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Map<String, Object> getUserBaseInfoForMap(Integer userId) throws Exception {
		JSONObject baseInfoJson = getUserBaseInfoForJson(userId);
		if(null == baseInfoJson || !baseInfoJson.getJSONObject("response").containsKey("info")
				|| baseInfoJson.getJSONObject("response").getJSONObject("info") == null) {
			return null;
		}
		Map<String,Object> userInfo = JSONObject.toJavaObject(baseInfoJson.getJSONObject("response").getJSONObject("info"), Map.class);
		String orderPhone = null == userInfo.get("orderPhone") ? "" : String.valueOf(userInfo.get("orderPhone"));
		if(!StringUtil.isEmpty(orderPhone)) {
			JSONArray phoneJson = JSONObject.parseArray(orderPhone);
			userInfo.put("orderPhone", phoneJson.toJSONString());
			List<Map<String,Object>> phones = new ArrayList<Map<String,Object>>();
			for(int i = 0; i<phoneJson.size(); i++) {
				Map<String,Object> phoneMap = new HashMap<String,Object>();
				phoneMap.put("phone", phoneJson.getJSONObject(i).get("phone"));
				phoneMap.put("isVerify", phoneJson.getJSONObject(i).get("isVerify"));
				phoneMap.put("isImport", phoneJson.getJSONObject(i).get("isImport"));
				phones.add(phoneMap);
			}
			userInfo.put("orderPhones", phones);
		}
		
		//根据用户默认收货地址获取具体地址省市区
		fillUserAddress(userInfo,userId);
		
		//解析用户标签
		fillUserTag(userInfo);
		
		return userInfo;
	}
	
	/**
	 * 根据用户默认收货地址填充用户居住地址
	 * @param userInfo
	 * @param userId
	 */
	private void fillUserAddress(Map<String, Object> userInfo, int userId) throws Exception {
		JSONObject usrAddress = getUserAddressList(1, 200, userId);
		if(usrAddress.containsKey("response")) {
			JSONArray jsarr = usrAddress.getJSONObject("response").getJSONArray("list");
			JSONObject addrObj = null;
			if(jsarr != null){
				for(int i = 0; i<jsarr.size(); i++) {
					if(jsarr.getJSONObject(i).getIntValue("isDefault") == 1) {
						addrObj = jsarr.getJSONObject(i);
						break;
					}
				}
			}
		
			if(null != addrObj) {
				String province = addrObj.getString("province");
				String city=addrObj.getString("city");
				String district = addrObj.getString("district");
				String areaCode = addrObj.getString("areaId");
				if(StringUtil.isEmpty(areaCode)) {
					JSONObject areaJson = getAreaInfo(province, city, district);
					areaCode = (null!=areaJson) ? areaJson.getString("areaId") : null;
				}
				
				if(!StringUtil.isEmpty(areaCode)) {
					userInfo.put("province", areaCode.substring(0, 2));
					userInfo.put("city", areaCode.substring(0, 4));
					userInfo.put("district", areaCode.substring(0, 6));
					userInfo.put("addrId", addrObj.get("id"));
				}
			}
		}
	}
	
	/**
	 * 将用户标签转换成map
	 * @param userInfo
	 */
	@SuppressWarnings("unchecked")
	private void fillUserTag(Map<String, Object> userInfo) {
		String tags = null == userInfo.get("tags") ? "" : String.valueOf(userInfo.get("tags"));
		JSONObject tagsJson = (JSONObject) JSONObject.parse(tags);
		Map<String,String> tagsMap = JSONObject.toJavaObject(tagsJson, Map.class);
		userInfo.put("tagsMap", tagsMap);
		
	}

	/**
	 * 注册
	 */
	@Override
	public JSONObject register(String userName, String nickName, String passwd, String confirmPasswd, String ip, String expandCode,
			Integer applicablePlatformId) {
		ApiUrl apiUrl = this.buildUcenterApiBaseUrl("USER_ACCOUNT_REGISTER");
		String[] otherParams = {ApiUrlParamEnum.username.getParamName(), userName,
				ApiUrlParamEnum.nickName.getParamName(), nickName,
				ApiUrlParamEnum.password.getParamName(), passwd, 
				ApiUrlParamEnum.ip.getParamName(), ip,
				ApiUrlParamEnum.confirmPassword.getParamName(), confirmPasswd, 
				ApiUrlParamEnum.applicablePlatformId.getParamName(), applicablePlatformId.toString(),
				ApiUrlParamEnum.expandCode.getParamName(), String.valueOf(expandCode)};
		String[] allParams = (String[]) ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		JSONObject result = null;
		try{
			result = (JSONObject)this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
		} catch (Exception e) {
			log.error("func[register] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(),Arrays.deepToString(allParams), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return result;
	}
	
	/**
	 * 登录
	 */
	@Override
	public JSONObject login(String username, String passwd, String ip) {
		ApiUrl apiUrl = this.buildUcenterApiBaseUrl("USER_LOGIN");
		String[] otherParams = {ApiUrlParamEnum.username.getParamName(), username, ApiUrlParamEnum.password.getParamName(), passwd, ApiUrlParamEnum.ip.getParamName(), ip};
		String[] allParams = (String[]) ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		JSONObject result = null;
		try{
			result = (JSONObject) this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
		} catch (Exception e) {
			log.error("func[login] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(),Arrays.deepToString(allParams), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return result;
	}
	
	/**
	 * 获取用户信息
	 */
	@Override
	public UserBaseAccountInfo getUser(Integer userId) {
		ApiUrl apiUrl = this.buildUcenterApiBaseUrl("USER_ACCOUNT_DETAIL");
		String[] otherParams = {ApiUrlParamEnum.userId.getParamName(), userId.toString(), 
								ApiUrlParamEnum.verify.getParamName()};
		String[] allParams = (String[]) ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		JSONObject result = null;
		try{
			result = (JSONObject) this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
			if (ApiResultEnum.SUCCESS.getId() != result.getJSONObject("response").getIntValue(ResultParamEnum.code.getParamName()))
				throw new IllegalStateException(result.getJSONObject("response").getString(ResultParamEnum.tip.getParamName()));
			UserBaseAccountInfo accountInfo = JSONObject.toJavaObject(result.getJSONObject("response").getJSONObject("userBaseAccountInfo"),UserBaseAccountInfo.class);
			return accountInfo;
		} catch (Exception e) {
			log.error("func[getUser] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(),Arrays.deepToString(allParams), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return null;
	}
	
	/**
	 * 是否登录
	 */
	@Override
	public Boolean islogin(Integer userId, String userIp) {
		ApiUrl apiUrl = this.buildUcenterApiBaseUrl("USER_VERIFY");
		String[] otherParams = {ApiUrlParamEnum.userId.getParamName(), userId.toString(), 
				ApiUrlParamEnum.ip.getParamName(), userIp};
		String[] allParams = (String[]) ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		JSONObject result = null;
		try{
			result = (JSONObject) this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
			if (ApiResultEnum.SUCCESS.getId() == result.getJSONObject("response").getIntValue(ResultParamEnum.code.getParamName()))
				return Boolean.TRUE;
		} catch (Exception e) {
			log.error("func[islogin] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(),Arrays.deepToString(allParams), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return Boolean.FALSE;
	}
	
	/**
	 * 退出
	 */
	@Override
	public Boolean logout(Integer userId) {
		ApiUrl apiUrl = this.buildUcenterApiBaseUrl("USER_LOGOUT");
		String[] otherParams = {ApiUrlParamEnum.userId.getParamName(), userId.toString(), };
		String[] allParams = (String[]) ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		JSONObject result = null;
		try{
			result = (JSONObject) this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
			if (ApiResultEnum.SUCCESS.getId() == result.getJSONObject("response").getIntValue(ResultParamEnum.code.getParamName()))
				return Boolean.TRUE;
		} catch (Exception e) {
			log.error("func[logout] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(),Arrays.deepToString(allParams), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return Boolean.FALSE;
	}
	
	/**
	 * 更新密码
	 */
	@Override
	public JSONObject updatePasswd(Integer userId, String oldPasswd, String passwd, String confirmPasswd,String ip) {
		ApiUrl apiUrl = this.buildUcenterApiBaseUrl("USER_ACCOUNT_UPDATE_PASSWORD");
		String[] otherParams = {ApiUrlParamEnum.userId.getParamName(), userId.toString(), 
				ApiUrlParamEnum.oldPassword.getParamName(), oldPasswd,
				ApiUrlParamEnum.password.getParamName(), passwd, 
				ApiUrlParamEnum.confirmPassword.getParamName(), confirmPasswd, 
				ApiUrlParamEnum.ip.getParamName(), ip};
		String[] allParams = (String[]) ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		JSONObject result = null;
		try{
			result = (JSONObject) this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
		} catch (Exception e) {
			log.error("func[updatePasswd] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(),Arrays.deepToString(allParams), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return result;
	}
	
	/**
	 * 检测用户是否可用
	 */
	@Override
	public Boolean verifyName(String accountName) {
		ApiUrl apiUrl = this.buildUcenterApiBaseUrl("USER_VERIFY_NAME");
		String[] otherParams = {ApiUrlParamEnum.accountName.getParamName(), accountName};
		String[] allParams = (String[]) ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		JSONObject result = null;
		try{
			result = (JSONObject) this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
			if (ApiResultEnum.SUCCESS.getId() == result.getJSONObject("response").getIntValue(ResultParamEnum.code.getParamName()))
				return Boolean.TRUE;
		} catch (Exception e) {
			log.error("func[verifyName] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(),Arrays.deepToString(allParams), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return Boolean.FALSE;
	}
	
	/**
	 * 检测邮箱是否可用
	 */
	@Override
	public Boolean verifyEmail(String email) {
		ApiUrl apiUrl = this.buildUcenterApiBaseUrl("USER_VERIFY_EMAIL");
		String[] otherParams = {ApiUrlParamEnum.email.getParamName(), email};
		String[] allParams = (String[]) ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		JSONObject result = null;
		try{
			result = (JSONObject) this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
			if (ApiResultEnum.SUCCESS.getId() == result.getJSONObject("response").getIntValue(ResultParamEnum.code.getParamName()))
				return Boolean.TRUE;
		} catch (Exception e) {
			log.error("func[verifyEmail] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(),Arrays.deepToString(allParams), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return Boolean.FALSE;
	}
	
	/**
	 * 检测手机号码是否可用
	 */
	@Override
	public Boolean verifyPhone(String phone) {
		ApiUrl apiUrl = this.buildUcenterApiBaseUrl("USER_VERIFY_PHONE");
		String[] otherParams = {ApiUrlParamEnum.phone.getParamName(), phone};
		String[] allParams = (String[]) ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		JSONObject result = null;
		try{
			result = (JSONObject) this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
			if (ApiResultEnum.SUCCESS.getId() == result.getJSONObject("response").getIntValue(ResultParamEnum.code.getParamName()))
				return Boolean.TRUE;
		} catch (Exception e) {
			log.error("func[verifyPhone] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(),Arrays.deepToString(allParams), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return Boolean.FALSE;
	}
	
	/**
	 * 忘记密码
	 */
	@Override
	public JSONObject findPwd(String accountName, String email, String phone, String ip) {
		ApiUrl apiUrl = this.buildUcenterApiBaseUrl("USER_LOST_PASSWORD_BY_ACCOUNT");
		String[] otherParams = {ApiUrlParamEnum.accountName.getParamName(), accountName, 
				ApiUrlParamEnum.email.getParamName(), email,
				ApiUrlParamEnum.ip.getParamName(), ip,
				ApiUrlParamEnum.phone.getParamName(), phone};
		String[] allParams = (String[]) ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		JSONObject result = null;
		try{
			result = (JSONObject) this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
			if (ApiResultEnum.SUCCESS.getId() != result.getJSONObject("response").getIntValue(ResultParamEnum.code.getParamName()))
				throw new IllegalStateException(result.getJSONObject("response").getString(ResultParamEnum.tip.getParamName()));
			result = result.getJSONObject("response");
		} catch (Exception e) {
			log.error("func[findPwd] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(),Arrays.deepToString(allParams), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
			return null;
		}
		return result;
	}
	
	/**
	 * 发送找回密码的邮件
	 */
	@Override
	public boolean sendEmailPwd(Integer userId, String email) {
		ApiUrl apiUrl = this.buildUcenterApiBaseUrl("USER_LOST_PASSWORD_BY_EMAIL");
		String[] otherParams = {ApiUrlParamEnum.userId.getParamName(), userId.toString(), ApiUrlParamEnum.email.getParamName(), email};
		String[] allParams = (String[]) ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		JSONObject result = null;
		try{
			result = (JSONObject) this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
			if (ApiResultEnum.SUCCESS.getId() == result.getJSONObject("response").getIntValue(ResultParamEnum.code.getParamName()))
				return Boolean.TRUE;
		} catch (Exception e) {
			log.error("func[sendEmailPwd] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(),Arrays.deepToString(allParams), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return Boolean.FALSE;
	}
	
	/**
	 * 重置密码
	 */
	@Override
	public boolean resetPasswd(String userId, String key, String password, String confirmPasswd, String ip) {
		ApiUrl apiUrl = this.buildUcenterApiBaseUrl("USER_RESET_USER_PASSWORD");
		String[] otherParams = {ApiUrlParamEnum.userId.getParamName(), userId, 
				ApiUrlParamEnum.key.getParamName(), key,
				ApiUrlParamEnum.password.getParamName(), password,
				ApiUrlParamEnum.ip.getParamName(), ip, 
				ApiUrlParamEnum.confirmPassword.getParamName(), confirmPasswd};
		String[] allParams = (String[]) ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		JSONObject result = null;
		try{
			result = (JSONObject) this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
			if (ApiResultEnum.SUCCESS.getId() == result.getJSONObject("response").getIntValue(ResultParamEnum.code.getParamName()))
				return Boolean.TRUE;
		} catch (Exception e) {
			log.error("func[resetPasswd] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(),Arrays.deepToString(allParams), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return Boolean.FALSE;
	}
	
	/**
	 * 根据手机号码重置密码
	 */
	@Override
	public boolean resetPasswdByPhone(String userId, String phone, String key, String password, String confirmPasswd, String ip) {
		ApiUrl apiUrl = this.buildUcenterApiBaseUrl("USER_RESET_PASSWORD_BY_PHONE");
		String[] otherParams = {ApiUrlParamEnum.userId.getParamName(), userId, 
				ApiUrlParamEnum.phone.getParamName(), phone,
				ApiUrlParamEnum.key.getParamName(), key, 
				ApiUrlParamEnum.password.getParamName(), password,
				ApiUrlParamEnum.ip.getParamName(), ip,
				ApiUrlParamEnum.confirmPassword.getParamName(), confirmPasswd};
		String[] allParams = (String[]) ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		JSONObject result = null;
		try{
			result = (JSONObject) this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
			if (ApiResultEnum.SUCCESS.getId() == result.getJSONObject("response").getIntValue(ResultParamEnum.code.getParamName()))
				return Boolean.TRUE;
		} catch (Exception e) {
			log.error("func[resetPasswdByPhone] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(),Arrays.deepToString(allParams), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return Boolean.FALSE;
	}
	
	public JSONObject bindEmail(Integer userId, String email, String ip) {
		ApiUrl apiUrl = this.buildUcenterApiBaseUrl("USER_BIND_EMAIL");
		String[] otherParams = {ApiUrlParamEnum.userId.getParamName(), userId.toString(),
				ApiUrlParamEnum.email.getParamName(), email,
				ApiUrlParamEnum.ip.getParamName(), ip,
				};
		String[] allParams = (String[]) ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		JSONObject result = null;
		try{
			result = (JSONObject) this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
			if (ApiResultEnum.SUCCESS.getId() == result.getJSONObject("response").getIntValue(ResultParamEnum.code.getParamName()))
				return result;
		} catch (Exception e) {
			log.error("func[bindEmail] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(),Arrays.deepToString(allParams), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return result;
	}
	
	public JSONObject unbindEmail(Integer userId, String email) {
		ApiUrl apiUrl = this.buildUcenterApiBaseUrl("USER_UNBIND_EMAIL");
		String[] otherParams = {ApiUrlParamEnum.userId.getParamName(), userId.toString(),
				ApiUrlParamEnum.email.getParamName(), email, 
				};
		String[] allParams = (String[]) ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		JSONObject result = null;
		try{
			result = (JSONObject) this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
			if (ApiResultEnum.SUCCESS.getId() == result.getJSONObject("response").getIntValue(ResultParamEnum.code.getParamName()))
				return result;
		} catch (Exception e) {
			log.error("func[unbindEmail] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(),Arrays.deepToString(allParams), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return result;
	}
	
	@Override
	public JSONObject bindPhone(Integer userId, String phone, String ip) {
		ApiUrl apiUrl = this.buildUcenterApiBaseUrl("USER_BIND_PHONE");
		String[] otherParams = {ApiUrlParamEnum.userId.getParamName(), userId.toString(),
				ApiUrlParamEnum.phone.getParamName(), phone,
				ApiUrlParamEnum.ip.getParamName(), ip,
				};
		String[] allParams = (String[]) ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		JSONObject result = null;
		try{
			result = (JSONObject) this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
		} catch (Exception e) {
			log.error("func[bindPhone] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(),Arrays.deepToString(allParams), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return result;
	}

	@Override
	public JSONObject unbindPhone(Integer userId, String phone) {
		ApiUrl apiUrl = this.buildUcenterApiBaseUrl("USER_UNBIND_PHONE");
		String[] otherParams = {ApiUrlParamEnum.userId.getParamName(), userId.toString(),
				ApiUrlParamEnum.phone.getParamName(), phone,
				};
		String[] allParams = (String[]) ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		JSONObject result = null;
		try{
			result = (JSONObject) this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
			if (ApiResultEnum.SUCCESS.getId() == result.getJSONObject("response").getIntValue(ResultParamEnum.code.getParamName()))
				return result;
		} catch (Exception e) {
			log.error("func[unbindPhone] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(),Arrays.deepToString(allParams), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return result;
	}
	
	/**
	 * 手机动态密码登录
	 */
	@Override
	public JSONObject loginByDynamicPassword(String username, String dynamicPassword, String ip, Integer applicablePlatformId) {
		ApiUrl apiUrl = this.buildUcenterApiBaseUrl("USER_LOGIN_DYNAMIC_PASSWORD");
		String[] otherParams = {
				ApiUrlParamEnum.username.getParamName(), username, 
				ApiUrlParamEnum.dynamicPassword.getParamName(), dynamicPassword, 
				ApiUrlParamEnum.ip.getParamName(), ip,
				ApiUrlParamEnum.applicablePlatformId.getParamName(), applicablePlatformId.toString()
				};
		String[] allParams = (String[]) ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		JSONObject result = null;
		try{
			result = (JSONObject) this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
		} catch (Exception e) {
			log.error("func[loginByDynamicPassword] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(),Arrays.deepToString(allParams), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return result;
	}
	
	/**
	 * 发送手机动态密码
	 */
	@Override
	public boolean sendDynamicPaswordByPhone(String username, String ip) {
		ApiUrl apiUrl = this.buildUcenterApiBaseUrl("USER_SEND_DYNAMIC_PASSWORD");
		String[] otherParams = {ApiUrlParamEnum.username.getParamName(), username, ApiUrlParamEnum.ip.getParamName(), ip};
		String[] allParams = (String[]) ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		JSONObject result = null;
		try{
			result = (JSONObject) this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
			if (ApiResultEnum.SUCCESS.getId() == result.getJSONObject("response").getIntValue(ResultParamEnum.code.getParamName()))
				return Boolean.TRUE;
		} catch (Exception e) {
			log.error("func[sendDynamicPaswordByPhone] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(),Arrays.deepToString(allParams), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return Boolean.FALSE;
	}
	
	/**
	 * 判断该用户是否已绑定手机号码
	 */
	@Override
	public boolean isBindPhoneByUsername(String username) {
		ApiUrl apiUrl = this.buildUcenterApiBaseUrl("USER_VERIFY_BIND_PHONE_BY_USERNAME");
		String[] otherParams = {ApiUrlParamEnum.username.getParamName(), username};
		String[] allParams = (String[]) ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		JSONObject result = null;
		try{
			result = (JSONObject) this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
			log.info("func[isBindPhoneByUsername] result[" + result + "]");
			if (ApiResultEnum.SUCCESS.getId() == result.getJSONObject("response").getIntValue(ResultParamEnum.code.getParamName()))
				return Boolean.TRUE;
		} catch (Exception e) {
			log.error("func[isBindPhoneByUsername] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(),Arrays.deepToString(allParams), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return Boolean.FALSE;
	}

	@Override
	public JSONObject checkUserBaseInfo(Integer userId) throws Exception {
		
		ApiUrl apiUrl = this.buildUcenterApiBaseUrl("USER_BASEINFO_CHECK");
		String[] otherParams = {ApiUrlParamEnum.userId.getParamName(), userId + ""};
		String[] allParams = (String[]) ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		JSONObject result = null;
		try{
			result = (JSONObject) this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
			log.info("func[checkUserBaseInfo] result[" + result + "]");
		} catch (Exception e) {
			log.error("func[checkUserBaseInfo] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(),Arrays.deepToString(allParams), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return result;
	}
}
